package com.atguigu.tingshu.user.login;/*
 * title:
 * author:
 * date:
 * description:
 * */

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.atguigu.tingshu.account.client.UserAccountFeignClient;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.service.KafkaService;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import jakarta.annotation.Resource;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@BeanType(LoginType.WEIXIN)
public class WxLogin implements ILogin{
    @Resource
    private UserAccountFeignClient userAccountFeignClient;
    @Resource
    private WxMaService wxMaService;
    @Resource
    private RedisTemplate redisTemplate;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Resource
    private KafkaService kafkaService;
    @Override
    public Map<String, Object> login(Map<String, Object> map) {
        String code = map.get("code").toString();
        try {
            WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(code);
            String openid = sessionInfo.getOpenid();
            UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getWxOpenId, openid));
            if (userInfo==null){
                userInfo = new UserInfo();
                userInfo.setNickname("这家伙很懒"+ IdWorker.getIdStr());
                userInfo.setWxOpenId(openid);
                userInfo.setAvatarUrl("https://img2.baidu.com/it/u=396436058,219850851&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500");
                userInfo.setStatus("1");
                userInfoMapper.insert(userInfo);
//                userAccountFeignClient.saveUserAccount(userInfo.getId());
                kafkaService.sendMsg(KafkaConstant.QUEUE_USER_REGISTER,userInfo.getId().toString());

            }

            String token = UUID.randomUUID().toString().replaceAll("-", "");
            UserInfoVo userInfoVo=new UserInfoVo();
            BeanUtils.copyProperties(userInfo,userInfoVo);
            redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX+token,userInfoVo,RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);
            Map resultMap=new HashMap<>();
            resultMap.put("token",token);
            return resultMap;
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("微信登录失败！");
    }
}
